package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"xialou_platform/app/requestx"

	"xialou_platform/services/wallet/rpc/internal/svc"
	"xialou_platform/services/wallet/rpc/walletClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type WithdrawalListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewWithdrawalListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WithdrawalListLogic {
	return &WithdrawalListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 提现列表
func (l *WithdrawalListLogic) WithdrawalList(in *walletClient.WithdrawalListReq) (*walletClient.WithdrawalListResp, error) {
	count := l.svcCtx.WalletWithdrawalModel.FindListCount(l.ctx, in.Keyword, in.Type, in.Status)

	res := make([]*walletClient.WithdrawalResp, 0)
	if count > 0 {
		list, err := l.svcCtx.WalletWithdrawalModel.FindList(l.ctx, &requestx.ListReq{
			Page:     in.Page,
			PageSize: in.PageSize,
			Keyword:  in.Keyword,
		}, in.Type, in.Status)
		if err != nil {
			l.Error(err.Error())
			return nil, err
		}
		for _, withdrawal := range list {
			temp := &walletClient.WithdrawalResp{}
			copier.Copy(&temp, withdrawal)
			temp.CreateTime = withdrawal.CreateTime.Unix()
			temp.Status = withdrawal.Status
			temp.PaymentTime = withdrawal.UpdateTime.Unix()

			wallet, err := l.svcCtx.WalletModel.FindOne(l.ctx, withdrawal.WalletId)
			if err != nil {
				l.Error(err.Error())
				continue
				//return nil, err
			}
			temp.SourceType = wallet.SourceType
			temp.BankName = wallet.BankName
			temp.BankNumber = wallet.BankNumber
			temp.AccountName = wallet.AccountName
			temp.Remark = withdrawal.Remark
			res = append(res, temp)
		}
	}
	return &walletClient.WithdrawalListResp{
		Total: count,
		List:  res,
	}, nil
}
